/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ar.edu.utn.frsf.cidisi.ginet.abstractginet.impl;

import ar.edu.utn.frsf.cidisi.ginet.abstractginet.AbstractginetPackage;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Arc;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Page;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Place;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.RefPlace;
import ar.edu.utn.frsf.cidisi.ginet.abstractginet.Transition;

import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.pnml.tools.epnk.pnmlcoremodel.Node;
import org.pnml.tools.epnk.pnmlcoremodel.NodeGraphics;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Page</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class PageImpl extends ar.edu.utn.frsf.cidisi.ginet.impl.PageImpl implements Page {
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected PageImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return AbstractginetPackage.Literals.PAGE;
	}
	

	@Override
	public EList<Place> getPlaces() {
		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		EList<Place> places = new BasicEList<Place>();
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof Place){
			
				Place place = (Place)element;
				
//				if (place.getRefPlace()==null)
					places.add(place);	
			}
		}
		
		return places;
	}

//	@Override
//	public EList<RefPlace> getRefPlaces() {
//		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
//		EList<RefPlace> refPlaces = new BasicEList<RefPlace>();
//		
//		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
//			
//			if (element instanceof RefPlace){
//			
//				RefPlace refPlace = (RefPlace)element;
//			
//				refPlaces.add(refPlace);	
//			}
//		}
//		
//		return refPlaces;
//	}

	@Override
	public EList<RefPlace> getRefPlaces() {
		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		EList<RefPlace> refPlaces = new BasicEList<RefPlace>();
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof RefPlace){
			
				RefPlace refPlace = (RefPlace)element;
				
//				if (refPlace.getRef()!=null)
					refPlaces.add(refPlace);	
			}
		}
		
		return refPlaces;
	}


	@Override
	public EList<Transition> getTransitions() {
		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		EList<Transition> transitions = new BasicEList<Transition>();
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof Transition){
			
				Transition transition = (Transition)element;
			
				transitions.add(transition);	
			}
		}
		
		return transitions;
	}

	@Override
	public EList<Arc> getArcs() {
		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		EList<Arc> arcs = new BasicEList<Arc>();
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof Arc){
			
				Arc arc = (Arc)element;
			
				arcs.add(arc);	
			}
		}
		
		return arcs;
	}

	@Override
	public Place getPlace(String id) {
		
		EList<Place> places = getPlaces();
		for (Place p : places){
			if (p.getId().equals(id)){
				return p;
			}
		}
		
		return null;
	}

	@Override
	public Transition getTransition(String id) {

		EList<Transition> transitions = getTransitions();
		for (Transition t : transitions){
			if (t.getId().equals(id)){
				return t;
			}
		}
		
		return null;

	}

	@Override
	public Arc getArc(String id) {

		EList<Arc> arcs = getArcs();
		for (Arc a : arcs){
			if (a.getId().equals(id)){
				return a;
			}
		}
		
		return null;

	}

	@Override
	public EList<Page> getPages() {
		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		EList<Page> pages = new BasicEList<Page>();
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof Page){
			
				Page page = (Page)element;
			
				pages.add(page);
				
			}
			
		}
		
		return pages;
	}

	@Override
	public EList<Transition> getReferringTransitions() {
		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		EList<Transition> transitions = new BasicEList<Transition>();
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof Transition){
				
				// If the element is a transition referencing a Page, then the referenced Page must be added to the list of pages
				if (((Transition)element).getRefPage() != null)
					transitions.add((Transition)element);

			}
		}
		
		return transitions;
	}

	@Override
	public Place getInputPlace() {
		
		EList<Place> places = getPlaces();
		
		for (Place place: places){
			
			if (place.isInput())
				return place;
		
		}
		
		return null;
	}

	
	@Override
	public float getMostRightXPos() {
		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		
		float xPos = 0;
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof Node){
				
				Node node = ((Node)element);
				
				if (((NodeGraphics)node.getGraphics()).getPosition().getX() > xPos)
					xPos = ((NodeGraphics)node.getGraphics()).getPosition().getX();
				
			}
			
		}
		
		return xPos;
	}

	@Override
	public Place getReferencedAbstractPlace(RefPlace refPlace){
		
		if (refPlace.getRef() != null){
			
			if (refPlace.getRef() instanceof Place){
				
				return (Place)refPlace.getRef();
				
			}
			else{
				
				return getReferencedAbstractPlace((RefPlace)refPlace.getRef());
				
			}
			
		}
		
		return (Place)refPlace.getRef();
	}

	@Override
	public Transition getReferringTransition(Page refAgim, RefPlace refPlace) {

		EList<org.pnml.tools.epnk.pnmlcoremodel.Object> elements = getObject();
		
		for (org.pnml.tools.epnk.pnmlcoremodel.Object element:elements){
			
			if (element instanceof Transition){
				
				Transition transition = (Transition)element;

				if (transition.getRefPage() != null && transition.getRefPage() instanceof Page){
					
					Page page = (Page)transition.getRefPage();
					
					if (page.getId().equals(refAgim.getId()) && transition.isConnectedTo(refPlace))
						return transition;
						
				}

			}

		}
		
		return null;
	}



} //PageImpl
